﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class _ISYC_303_07
    {
        public static void ISYC_303_07(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double scale = 3; //放大系数

            double oal = Convert.ToDouble(data["oal"]) * scale;
            double lpr = Convert.ToDouble(data["lpr"]) * scale;
            double lh = Convert.ToDouble(data["lh"]) * scale;
            double apmx = Convert.ToDouble(data["apmx"]) * scale;
            double prfa = Convert.ToDouble(data["prfa"]);
            double dcx = Convert.ToDouble(data["dcx"]) * scale;
            double bd = Convert.ToDouble(data["bd"]) * scale;
            double ls = Convert.ToDouble(data["ls"]) * scale;
            double dconms = Convert.ToDouble(data["dconms"]) * scale;
            double dn = Convert.ToDouble(data["dn"]) * scale;

            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + oal + 6, v1.Y);
            Vector2 v3 = new Vector2(v1.X + 3, v1.Y);
            Vector2 v4 = new Vector2(v3.X, v3.Y + dconms / 2.276);
            Vector2 v5 = new Vector2(v3.X + (dconms / 2 - v4.Y), v3.Y + dconms / 2);
            Vector2 v6 = new Vector2(v3.X + ls, v5.Y);
            Vector2 v7 = new Vector2(v6.X + 0.5, v3.Y + dn / 2);
            Vector2 v9 = new Vector2(v2.X - 3 - lh, v3.Y + bd / 2);
            Vector2 v8 = new Vector2(v9.X - 0.35, v7.Y);
            Vector2 v10 = new Vector2(v9.X + lh / 11.038, v3.Y + dcx / 2);
            Vector2 v11 = new Vector2(v10.X, v10.Y * -1);
            Vector2 v12 = new Vector2(v9.X, v9.Y * -1);
            Vector2 v13 = new Vector2(v8.X, v8.Y * -1);
            Vector2 v14 = new Vector2(v7.X, v7.Y * -1);
            Vector2 v15 = new Vector2(v6.X, v6.Y * -1);
            Vector2 v16 = new Vector2(v5.X, v5.Y * -1);
            Vector2 v17 = new Vector2(v4.X, v4.Y * -1);
            Vector2 v18 = new Vector2(v2.X - 3 - lpr, v3.Y);
            Vector2 v19 = new Vector2(v18.X, v18.Y + dconms / 2 + 1);
            Vector2 v20 = new Vector2(v19.X, v19.Y * -1);
            Vector2 v36 = new Vector2(v3.X + oal, v3.Y);
            Vector2 v35 = new Vector2(v36.X - lh / 15.992, v36.Y - bd / 3.173);
            Vector2 v40 = new Vector2(v36.X - lh / 13.723, v36.Y + bd / 3.389);

            //作图
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v40));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            line2.Vertexes.Add(new LwPolylineVertex(v9));
            line2.Vertexes.Add(new LwPolylineVertex(v12));
            line2.Vertexes.Add(new LwPolylineVertex(v11));
            line2.Vertexes.Add(new LwPolylineVertex(v35));
            fun.PolyFill(dxf, line2, 0.1, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v8));
            line3.Vertexes.Add(new LwPolylineVertex(v9));
            fun.PolyFill(dxf, line3, 0.1, contour);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v13));
            line4.Vertexes.Add(new LwPolylineVertex(v8));
            line4.Vertexes.Add(new LwPolylineVertex(v9));
            line4.Vertexes.Add(new LwPolylineVertex(v12));
            line4.Vertexes.Add(new LwPolylineVertex(v13));
            line4.Vertexes.Add(new LwPolylineVertex(v14));
            line4.Vertexes.Add(new LwPolylineVertex(v7));
            line4.Vertexes.Add(new LwPolylineVertex(v8));
            fun.PolyFill(dxf, line4, 0.1, contour);

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(v6));
            line5.Vertexes.Add(new LwPolylineVertex(v15));
            line5.Vertexes.Add(new LwPolylineVertex(v16));
            line5.Vertexes.Add(new LwPolylineVertex(v5));
            line5.Vertexes.Add(new LwPolylineVertex(v6));
            line5.Vertexes.Add(new LwPolylineVertex(v7));
            line5.Vertexes.Add(new LwPolylineVertex(v14));
            line5.Vertexes.Add(new LwPolylineVertex(v15));
            fun.PolyFill(dxf, line5, 0.1, contour);

            LwPolyline line6 = new LwPolyline();
            line6.Vertexes.Add(new LwPolylineVertex(v5));
            line6.Vertexes.Add(new LwPolylineVertex(v4));
            line6.Vertexes.Add(new LwPolylineVertex(v17));
            line6.Vertexes.Add(new LwPolylineVertex(v16));
            fun.PolyFill(dxf, line6, 0.1, contour);

            Arc c7 = fun.DrawArc3(v35, v36, v40);
            fun.drawBArc(dxf, c7, 0.1, contour);


            //标注

            double height = 2;
            double arrowsize = 2;
            double exline = 2;



            fun.dimrot1(dxf, v36, v4, 0, oal / 2, 1, height, arrowsize, exline, "OAL", "", "");

            fun.dimrot1(dxf, v19, v36, 0, oal / 3, 1, height, arrowsize, exline, "LPR", "", "");

            fun.dimrot1(dxf, v12, v36, 180, oal / 4, 1, height, arrowsize, exline, "LH", "", "");

            fun.dimrot1(dxf, v11, v36, 180, oal / 6, 1, height, arrowsize, exline, "APMX", "", "");

            fun.dimrot1(dxf, v5, v16, 90, dconms, 1, height, arrowsize, exline, "DCONMS", "", "");

            fun.dimrot1(dxf, v7, v14, -90, dconms, 1, height, arrowsize, exline, "DN", "", "");

            fun.dimrot1(dxf, v17, v15, 180, oal / 6, 1, height, arrowsize, exline, "LS", "", "");

            fun.dimrot1(dxf, v9, v12, -90, oal * 4 / 3, 1, height, arrowsize, exline, "BD", "", "");

            fun.dimrot1(dxf, v10, v11, -90, oal, 1, height, arrowsize, exline, "DCX", "", "");

            fun.dimrot1(dxf, v40, v35, -90, apmx / 2, 1, height, arrowsize, exline, "DC", "", "");

            fun.jdbz2(dxf, new Line(v40, fun.Polar(v40, (180 - prfa / 2) * pi / 180, 0.1)), new Line(v35, fun.Polar(v35, (prfa / 2 - 180) * pi / 180, 0.1)), oal + apmx / 2, height, arrowsize, exline, "PRFA", "", "");


            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}